package cc.mrbird.febs.cos.controller;


import cc.mrbird.febs.common.utils.R;
import cc.mrbird.febs.cos.entity.AccessInfo;
import cc.mrbird.febs.cos.service.IAccessInfoService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * @author fanke
 */
@RestController
@RequestMapping("/cos/access-info")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AccessInfoController {

    private final IAccessInfoService accessInfoService;

    /**
     * 分页查询
     * @param page
     * @param accessInfo
     * @return
     */
    @GetMapping("/page")
    public R getBooKInfoByPage(Page page, AccessInfo accessInfo) {
        return R.ok(accessInfoService.page(page, Wrappers.<AccessInfo>lambdaQuery()
                .gt(accessInfo.getStartDate()!=null && accessInfo.getEndDate() != "",AccessInfo::getAccessTime,accessInfo.getStartDate())
                .lt(accessInfo.getEndDate()!=null && accessInfo.getEndDate() != "",AccessInfo::getAccessTime,accessInfo.getEndDate())
                .like(accessInfo.getAccessArea()!=null && accessInfo.getAccessArea() != "",AccessInfo::getAccessArea,accessInfo.getAccessArea())
                .orderByDesc(AccessInfo::getAccessTime)));
    }

    /**
     * 新增
     * @param accessInfo
     * @return
     */
    @PostMapping
    public R save(AccessInfo accessInfo) {
        return R.ok(accessInfoService.save(accessInfo));
    }

}
